<?php namespace App\Admin\Orders\Orders\Listeners;

use App\Core\Service\Listeners\DetailListener as Base;
use App\Core\Repository\Criterias\FixedCriteria;
//商品
use App\Admin\Goods\Goods\Repository as GoodsRepository;

class DetailListener extends Base {
    
    public function onDetailAfterGet(){
        $detail = $this->service->detailModel;
        if($detail->type == 7){
            $orderInfos = $detail->order_info ? json_decode($detail->order_info): '';
            if($orderInfos){
                $orderInfoText = [];
                foreach ($orderInfos as $orderInfo){
                    if(!$orderInfo){
                        continue;
                    }
                    $goodsName = $this->getGoodsName($orderInfo->goodId);
                    $orderInfoText[] = [
                        'goodsName' => $goodsName,
                        'num' => $orderInfo->num,
                        'price' => $orderInfo->price,
                        'socre' => $orderInfo->socre
                    ];
                }
                $detail->orderInfoText = $orderInfoText;
            }
        }
    }
    
    private function getGoodsName($id){
        $goods = with(new GoodsRepository)->addCriteria(new FixedCriteria('id', $id))->first('name');
        $goodsName = $goods->name;
        return $goodsName;
    }
}